package com.etoak.common.web.context;

import com.etoak.common.jwt.JwtUtil;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Map;

public class LoginContext {

    /**
     * 先获取请求
     *
     * @return
     */
    public static String getUserId() {
        String header = getRequest().getHeader(HttpHeaders.AUTHORIZATION);
        Map<String, Object> parse = JwtUtil.parse(header);
        return parse.get("id").toString();

    }

    public static HttpServletRequest getRequest() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return requestAttributes.getRequest();
    }

}
